package com.ddn.service.logistics.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;

import com.ddn.mapper.logistics.HqManagerMapper;
import com.ddn.mapper.logistics.HqPurchasingMapper;
import com.ddn.mapper.logistics.dto.HqPurchasingExportyDto;
import com.ddn.model.logistics.HqManage;
import com.ddn.model.logistics.HqPurchasing;
import com.ddn.service.logistics.HqPurchasingService;
import com.ddn.util.StringUtil;
import com.ddn.vo.PageBean;
import com.ddn.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author zlp
 * @date 2022/8/29 9:13
 */
@Service
public class HqPurchasingServiceImpl implements HqPurchasingService {
    @Autowired
    private HqPurchasingMapper dao;
    @Autowired
    private HqManagerMapper managerDao;


    @Override
    public R save(HqPurchasing purchasing) {
        //新增采购
        if(dao.save(purchasing)>0){
            //新增库存

            HqManage hqManage = new HqManage(null, purchasing.getName(), null, purchasing.getNum(), new Date(), "后勤主管");
            managerDao.save(hqManage);

              return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R delete(int id) {
        if(dao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(HqPurchasing purchasing) {
        if(dao.update(purchasing)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBean pageByName(String name, int page, int limit) {
        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        int start=(page-1)*limit;
        return  new PageBean((int) dao.selectCount(name),dao.selectPage(name,start,limit));
    }

    @Override
    public PageBean unByName(String name, int page, int limit) {
        System.out.println("**********采购"+name);
        if(StringUtil.isNotEmpty(name)){
            name="%"+name+"%";
        }
        int start=(page-1)*limit;
        return  new PageBean((int) dao.selectCount(name),dao.seunionPage(name,start,limit));
    }

    @Override
    public List<HqPurchasingExportyDto> all() {
        return dao.all();
    }

    @Override
    public R upload(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            List<HqPurchasing > list= EasyExcel.read(file.getInputStream(), HqPurchasing.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            if(dao.saveBatch(list) >0){
                return  R.ok();
            }
        }
        return  R.fail();
    }

    @Override
    public R delByIds(int[] ids) {
        if(dao.delByIds(ids)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }
}
